#include <cstdio>
#include <stdio.h>      
#include <sys/types.h>
#include <ifaddrs.h>
#include <netinet/in.h> 
#include <string.h> 
#include <arpa/inet.h>
#include<net/if.h>
#include<sys/socket.h>
#include <sys/ioctl.h>

using namespace std;

int main(int argc, char *argv[])
{
	int inet_sock;
	struct ifreq ifr;
					 
	//建立套接字
	inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
	  
	//获得接口地址
	bzero(&ifr, sizeof(ifr));
	strcpy(ifr.ifr_name, "eth0");
	if (ioctl(inet_sock, SIOCGIFADDR, &ifr) < 0)
		perror("ioctl");
	printf("host:%s\n", inet_ntoa(((struct sockaddr_in*)&(ifr.ifr_addr))->sin_addr));
	//获得广播地址
	bzero(&ifr, sizeof(ifr));
	strcpy(ifr.ifr_name, "eth0");
	if (ioctl(inet_sock, SIOCGIFBRDADDR, &ifr) < 0)
	perror("ioctl");
	printf("broadcast:%s\n", inet_ntoa(((struct sockaddr_in*)&(ifr.ifr_addr))->sin_addr));
	
	return 0;
}
